package cn.yourkingda.module;

import java.util.ArrayList;
import java.util.List;

import org.nutz.ioc.annotation.InjectName;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import cn.yourkingda.model.SaleOrderM;
import cn.yourkingda.pojo.Food;
import cn.yourkingda.pojo.Saleorder;
import cn.yourkingda.service.SaleorderService;
import cn.yourkingda.service.SaleorderitemService;
import cn.yourkingda.util.MyJson;

import com.alibaba.fastjson.JSON;

@InjectName("saleorderModule")
@Fail("json")
public class SaleorderModule {
	private SaleorderService saleorderService;
	private SaleorderitemService saleorderitemService;

	/**
	 * 更新菜品类别
	 */
	@At("/mvc/saleorder/update")
	@Ok("json")
	public MyJson updateSaleorder(@Param("data") String str) throws Exception{
		MyJson myJson = new MyJson();
		SaleOrderM json = JSON.parseObject(str,SaleOrderM.class);
		//插入saleorder
		Saleorder saleorder = new Saleorder();
		saleorder.setCreatdatetime(json.getCreateTime());
		saleorder.setCustomerId(json.getCustomerId());
		saleorder.setSaleorderno(json.getOrderNo());
		saleorder.setTotal(json.getTotal());
		saleorder.setState(0);
		//插入saleorder表
		try{
			saleorderService.updateSaleorder(saleorder);
		}catch(Exception e){
			e.printStackTrace();
			myJson.setStatus(0);
			myJson.setMsg("请检查所有输入的数");
			return myJson;
		}
		String[] foodList = json.getFoodList();
		List<Food> foodList2 = new ArrayList();
		for(int i=0;i<foodList.length;i++){
			String[] food = foodList[i].split(",");
			Food afood = new Food();
			afood.setId(Integer.parseInt(food[0]));
			afood.setPrice(food[1]);
			afood.setNum(Integer.parseInt(food[2]));
			foodList2.add(afood);
		}
		//插入saleitem表
		try{
			saleorderitemService.updateSaleorderitem(saleorder.getId(), foodList2);
		}catch(Exception e){
			e.printStackTrace();
			myJson.setStatus(0);
			myJson.setMsg("请检查所有输入的数");
			return myJson;
		}
		
		myJson.setStatus(1);
		myJson.setMsg("ok");
		return myJson;
	}
	/**
	 * 展示数据
	 */
	@At("/mvc/saleorder/list")
	@Ok("json")
	public List list(){
		return saleorderService.listSaleorder();
	}
	/**
	 * 删除数据
	 */
	@At("/mvc/saleorder/del")
	@Ok("json")
	public MyJson del(@Param("id") int id){
		int res =saleorderService.delSaleorder(id);
		MyJson myJson = new MyJson();
		if(res>0){
			myJson.setMsg("删除成功");
		}else{
			myJson.setMsg("删除失败");
		}
		myJson.setStatus(res);
		return myJson;
	}
	
	
	
	
}
